package dht.entity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Vector;

import dht.IClientRouteTable;
import dht.IDataServerRouteTable;
import dht.IEntity;

public class Message implements IEntity {

	private static final long serialVersionUID = 1L;
	private String transactionId;
	private MessageType msgType;
	private NodeInfo senderNodeInfo;
	private int msgSize;

	private AccessMessage accessMsg;
	private TableMessage tableMsg;
	private MoveMessage moveMsg;
	private ReportMessage reportMsg;

	public Message(String transactionId, MessageType msgType, NodeInfo nodeInfo) {
		this.transactionId = transactionId;
		this.msgType = msgType;
		this.senderNodeInfo = nodeInfo;
		this.msgSize = 0;
		this.accessMsg = new AccessMessage();
		this.tableMsg = new TableMessage();
		this.moveMsg = new MoveMessage();
		this.reportMsg = new ReportMessage();
	}

	public String getTransactionId() {
		return transactionId;
	}

	public void setTransactionId(String transactionId) {
		this.transactionId = transactionId;
	}

	public MessageType getMsgType() {
		return msgType;
	}

	public void setMsgType(MessageType msgType) {
		this.msgType = msgType;
	}

	public NodeInfo getSenderNodeInfo() {
		return senderNodeInfo;
	}

	public void setSenderNodeInfo(NodeInfo senderNodeInfo) {
		this.senderNodeInfo = senderNodeInfo;
	}

	public int getMessageSize() {
		if (msgSize > 0) {
			return msgSize;
		}
		msgSize = 0;
		try {
			ByteArrayOutputStream buf = new ByteArrayOutputStream(4096);
			ObjectOutputStream out = new ObjectOutputStream(buf);
			out.writeObject(this);
			out.flush();
			buf.close();
			msgSize = buf.size();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return msgSize;
	}

	public MoveMessage getMoveMsg() {
		return moveMsg;
	}

	public void setMoveMsg(MoveMessage moveMsg) {
		this.moveMsg = moveMsg;
	}

	public ReportMessage getReportMsg() {
		return reportMsg;
	}

	public void setReportMsg(ReportMessage reportMsg) {
		this.reportMsg = reportMsg;
	}

	public TableMessage getTableMsg() {
		return tableMsg;
	}

	public void setTableMsg(TableMessage tableMsg) {
		this.tableMsg = tableMsg;
	}

	public AccessMessage getAccessMsg() {
		return accessMsg;
	}

	public void setAccessMsg(AccessMessage accessMsg) {
		this.accessMsg = accessMsg;
	}

	public Vector<DataObject> getDataObjects() {
		return this.moveMsg.getDataObjects();
	}

	public void setDataObjects(Vector<DataObject> dataObjects) {
		this.moveMsg.setDataObjects(dataObjects);
	}

	public NodeInfo getMoveToServerInfo() {
		return this.moveMsg.getMoveToServerInfo();
	}

	public void setMoveToServerInfo(NodeInfo moveToServerInfo) {
		this.moveMsg.setMoveToServerInfo(moveToServerInfo);
	}

	public Vector<Integer> getMovementVnodeIds() {
		return this.moveMsg.getMovementVnodeIds();
	}

	public void setMovementVnodeIds(Vector<Integer> movementVnodeIds) {
		this.moveMsg.setMovementVnodeIds(movementVnodeIds);
	}

	public NodeInfo getMoveFromServerInfo() {
		return this.moveMsg.getMoveFromServerInfo();
	}

	public void setMoveFromServerInfo(NodeInfo moveFromServerInfo) {
		this.moveMsg.setMoveFromServerInfo(moveFromServerInfo);
	}

	public int getMoveId() {
		return this.moveMsg.getSubId();
	}

	public void setMoveId(int moveId) {
		this.moveMsg.setSubId(moveId);
	}

	public IClientRouteTable getClientRouteTable() {
		return this.tableMsg.getClientRouteTable();
	}

	public void setClientRouteTable(IClientRouteTable clientRouteTable) {
		this.tableMsg.setClientRouteTable(clientRouteTable);
	}

	public IDataServerRouteTable getDataServerRouteTable() {
		return this.tableMsg.getDataServerRouteTable();
	}

	public void setDataServerRouteTable(
			IDataServerRouteTable dataServerRouteTable) {
		this.tableMsg.setDataServerRouteTable(dataServerRouteTable);
	}

	public String getFileName() {
		return this.accessMsg.getBlockName();
	}

	public void setFileName(String fileName) {
		this.accessMsg.setBlockName(fileName);
	}

	public DataObject getDataObject() {
		return this.accessMsg.getDataObject();
	}

	public void setDataObject(DataObject dataObject) {
		this.accessMsg.setDataObject(dataObject);
	}

	public NodeInfo getFwdServerInfo() {
		return this.accessMsg.getFwdServerInfo();
	}

	public void setFwdServerInfo(NodeInfo fwdServerInfo) {
		this.accessMsg.setFwdServerInfo(fwdServerInfo);
	}

}
